<template>
  <GlobalContainer className="!bg-[#F3F3F3]" :backgroundImage="`${OSS_URL}/images/jiaofei/jiaofei_bg.png`">
    <GlobalHeader ref="headerRef" title="物品借用" :fixed="true" showBack :backgroundImage="`${OSS_URL}/images/jiaofei/jiaofei_bg.png`" />
    <div class="px-24">
      <div class="status-bar-container fixed" :style="{ top: `${headerHeight}` }">
        <div
          class="status-bar-item"
          v-for="(item, index) in statusBarList"
          :key="index"
          :class="statusBarActive === item.value && 'status-bar-item-active'"
          @tap="statusBarChange(item.value)"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="head-c h-110 py-30 relative w-full px-44" :style="{ marginTop: '100rpx' }">
        <image class="h-110 absolute left-0 top-0 w-full" :src="`${OSS_URL}/images/jieyong/head_bg.png`" />
        <image class="size-120 absolute right-36 top-0" :src="`${OSS_URL}/images/jieyong/head_bg_right.png`" />
        <div class="z-100 relative flex items-center" @tap.stop="toPageHandle">
          <span class="text-32 leading-48 text-white">选择借用物品</span>
          <image class="ml-10 size-32" src="../../static/images/home/loc_right.png" />
        </div>
      </div>

      <div class="jieyong-list mt-20">
        <div
          class="jieyong-item py-30 mb-20 pl-32 pr-44"
          v-for="item in borrowRecordList"
          :key="item.id"
          @tap="toJieyongDetailHandle(item)"
        >
          <div class="line1">
            <span class="text-30 leading-45 text-[#909399]">借用物品</span>
            <div class="status ml-auto" :class="getStatusClass(item.status)">{{ getStatusText(item.status) }}</div>
          </div>
          <div class="line2">
            <span class="text-32 leading-48 mt-24 font-normal text-[#303133]">{{ item.borrowGoodsDescription }}</span>
          </div>
          <div class="line3">
            <span class="text-28 leading-42 mt-12 font-normal text-[#909399]">{{ item.applyTime }}</span>
          </div>

          <div class="line4 text-28 leading-42 mt-12 font-normal" v-if="item.reason">
            <span class="text-[#303133]">拒绝原因：</span>
            <span class="break-all text-[#909399]">{{ item.reason }}</span>
          </div>
        </div>
      </div>
    </div>

    <div v-if="borrowRecordList.length === 0" class="empty-container mt-240 mx-auto flex flex-col items-center">
      <div class="empty-img mb-40">
        <image class="h-240 w-330" :src="`${OSS_URL}/images/jiaofei_empty.png`" />
      </div>
      <div class="empty-text text-28 leading-36 text-[#909399]">暂无记录</div>
    </div>
    <div v-else class="empty-container mt-18 text-28 leading-36 text-center text-[#909399]">已加载全部记录</div>
  </GlobalContainer>
</template>

<script setup>
import { ref, computed } from 'vue'
import { getBorrowRecordPage } from '@/common/api/report'
import { showLoading, hideLoading } from '@/utils'
const OSS_URL = import.meta.env.VITE_OSS_URL

const headerRef = ref()
const headerHeight = computed(() => {
  return headerRef.value?.wrapStyle?.height
})
const statusBarList = ref([
  {
    name: '全部',
    value: ''
  },
  {
    name: '待审核',
    value: 0
  },
  {
    name: '待归还',
    value: 1
  },
  {
    name: '已拒绝',
    value: 3
  }
])

const getStatusClass = status => {
  switch (status) {
    case 0:
      return 'status-wait'
    case 1:
      return 'status-ok'
    case 2:
      return 'status-ok'
    case 3:
      return 'status-not'
    default:
      return ''
  }
}

const getStatusText = status => {
  switch (status) {
    case 0:
      return '待审核'
    case 1:
      return '待归还'
    case 2:
      return '已归还'
    case 3:
      return '已拒绝'
    default:
      return ''
  }
}
const statusBarActive = ref('')

const toPageHandle = () => {
  uni.navigateTo({
    url: '/subPages/jieyongSelect/index'
  })
}

const borrowRecordList = ref([])

const statusBarChange = active => {
  statusBarActive.value = active
  getBorrowRecordPageHandle(active)
}
const getBorrowRecordPageHandle = async (status = '') => {
  showLoading()
  getBorrowRecordPage({ pageNo: 1, pageSize: 9999, status })
    .then(res => {
      borrowRecordList.value = res.records || []
    })
    .finally(() => {
      hideLoading()
      uni.stopPullDownRefresh()
    })
}

const toJieyongDetailHandle = item => {
  // Navigate to detail page with item data
}

onShow(() => {
  getBorrowRecordPageHandle(statusBarActive.value)
})

onPullDownRefresh(() => {
  getBorrowRecordPageHandle(statusBarActive.value)
})
</script>

<style scoped lang="scss">
.status-bar-container {
  width: 702rpx;
  left: 50%;
  transform: translateX(-50%);
  height: 80rpx;
  line-height: 1;
  text-align: center;
  background: #ffffff;
  border-radius: 10rpx 10rpx 10rpx 10rpx;
  padding: 24rpx 0;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.status-bar-item {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-color: #fff;
  font-weight: 400;
  font-size: 28rpx;
  color: #606266;
  border-right: 1px solid #e3e7ec;
}
.status-bar-item-active {
  color: #1e90ff;
}

.jieyong-list {
  .jieyong-item {
    background: #fcfeff;
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    border: 1rpx solid #ffffff;
    .line1,
    .line2,
    .line3 {
      display: flex;
      align-items: center;
    }
  }
}
.status {
  width: 118rpx;
  height: 52rpx;
  text-align: center;
  line-height: 52rpx;
  font-weight: 400;
  font-size: 26rpx;
  color: #ffffff;
  box-shadow: inset 0rpx 8rpx 16rpx 0rpx rgba(255, 255, 255, 0.3);
  border-radius: 12rpx 0rpx 12rpx 0rpx;
}
.status-wait {
  background: linear-gradient(303deg, #ff9933 0%, #ff8000 100%);
}
.status-ok {
  background: linear-gradient(303deg, #a3b4cc 0%, #8fa1b2 100%);
}
.status-not {
  background: linear-gradient(303deg, #ff5533 0%, #ff3333 100%);
}
</style>
